/*
** myfork.c for zappy in /home/guidet_m/TECK2/projets/zappy/server_zappy
** 
** Made by michael guidet
** Login   <guidet_m@epitech.net>
** 
** Started on  Mon Jul  4 15:35:59 2011 michael guidet
** Last update Mon Jul 11 10:14:42 2011 michael guidet
*/

#include "server.h"

void	start_fork(t_fd *fd, t_env *e, char *cmd)
{
  char	cmd2[BUF_SIZE];
  t_fd	*tmp;

  cmd = cmd;
  sprintf(cmd2, "pfk %d\n", fd->pl->id);
  for (tmp = e->fd; tmp; tmp = tmp->next)
    if (tmp->type == GRAPH)
      add_string_to_list(&tmp->send_list, cmd2);
  fd->wait = (long)(42.0f / (float)e->t * 1000000.0f);
  fd->idaction = 12;
}

void	graph_fork(t_egg *new, t_fd *fd, t_env *e)
{
  char	cmd2[BUF_SIZE];
  t_fd	*tmp;

  sprintf(cmd2, "enw %d %d %d %d\n", new->id, fd->pl->id, new->x, new->y);
  for (tmp = e->fd; tmp; tmp = tmp->next)
    if (tmp->type == GRAPH)
      add_string_to_list(&tmp->send_list, cmd2);
}

void	end_fork(t_fd *fd, t_env *e, char *cmd)
{
  int	i;
  t_egg	*new;

  printf("%sDrone %d %s Execute %s%s\n", PURPLE, fd->pl->id,
	 fd->pl->team, cmd, DEFAUT);
  fd->wait = -1;
  fd->idaction = -1;
  for (i = 0; e->teams[i].name; i++)
    if (!strcmp(e->teams[i].name, fd->pl->team))
      {
	new = x_null(NULL, malloc(sizeof(*new)), "malloc");
	new->id = e->new_id;
	e->new_id++;
	new->x = fd->pl->x;
	new->y = fd->pl->y;
	new->team = e->teams[i].name;
	new->time = (long)((600.0f / (float)e->t) * 1000000.0f);
	new->next = e->teams[i].eggs;
	e->teams[i].eggs = new;
	break ;
      }
  add_string_to_list(&fd->send_list, "ok\n");
  graph_fork(new, fd, e);
}
